import { ref } from 'vue'
import { Toast } from 'vant'
import { userStore } from "@/stores/registerCount";
import { requestRegister } from '@/api/register'
import { useRouter } from 'vue-router'
export const useRegister = () => {
    const registerData = ref({
        username: '',
        password: '',
        headerimg: '',
        nick: '',
        age: 0,
        sex: 0,
        visitgoal: '',
        education: '',
        company: '',
    })
    const actions = [
      { name: '确定' , color:"red"},
    ];
    const checked = ref(['0', '1'])
    const count = userStore()
    const show = ref(false)
    const router = useRouter()
    const onSubmit =async () => {
        registerData.value.age = Number(registerData.value.age)
        count.saveRegisterCount(registerData.value)
        
        const storeData = count.getRegisterCount
        const data = await requestRegister(storeData)
        const {resultCode,resultInfo} =data
        
        if (resultCode === 1) {
           show.value = true
           
        }else{
            Toast(resultInfo)
        }
        
    }
    
    const bindChange = () => {
        registerData.value.sex = Number(checked.value)
    }
    
    const onSelect = (item:any,index:any) => {
      // 默认情况下点击选项时不会自动收起
      // 可以通过 close-on-click-action 属性开启自动收起
      
      if (index === 0) {
        toLogin()
      }
      show.value = false;
    };
    const toLogin =()=>{
        router.replace('/')
    }
    return {
        registerData,
        onSubmit,
        checked,
        bindChange,
        show,
        onSelect,
        actions,
        toLogin,
    }
}
